package com.vgemv.jsutilitysdk.Alert;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.vgemv.jsutilitysdk.R;

import java.util.ArrayList;

public class JsAlertManager implements JsAlertManagerInterface {

    public static JsAlertManager _sharedInstance;
    public static Context mContext;

    public static void initClass(Context context) {
        if(_sharedInstance == null) {
            _sharedInstance = new JsAlertManager();
            mContext = context;
        }
    }
    public static JsAlertManager sharedInstance() {
        return _sharedInstance;
    }



//    //EditText本身就支持多行的处理.
//    public static void showInputDialog(final Context context, String title, String message, final JsEditTextDialogCallback callback) {
//        /*@setView 装入一个EditView
//         */
//        final EditText editText = new EditText(context);
//        AlertDialog.Builder inputDialog =
//                new AlertDialog.Builder(context);
//        inputDialog.setTitle(title);
//        inputDialog.setView(editText);
//        inputDialog.setMessage(message);
//        inputDialog.setNegativeButton("取消",
//                new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        if(callback != null) {
//                            callback.onCancel();
//                        }
//                    }
//                });
//        inputDialog.setPositiveButton("确定",
//                new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        if(callback != null) {
//                            callback.onConfirm(editText.getText().toString());
//                        }
//                    }
//                });
//        inputDialog.setCancelable(true);
//        inputDialog.show();
//    }

    /**
     * 产生一个有多个EditText的TextView
     * @param context context
     * @param title 标题
     * @param message 消息
     * @param placeholderArray 占位字符串,不能为空,根据这个值的数量决定edittext的个数.
     * @param defaultValueArray 默认值,可以为空
     * @param callback
     */
    public static void showEditTextDialog(Context context, String title, String message, @NonNull ArrayList<String> placeholderArray , @Nullable ArrayList<String>  defaultValueArray, final JsEditTextDialogCallback callback) {
//        final TextView textView = new TextView(context);
//        textView.setBackgroundColor(Color.parseColor("#ffcccccc"));

        AlertDialog.Builder inputDialog =
                new AlertDialog.Builder(context);
        inputDialog.setTitle(title);
        inputDialog.setMessage(message);

        //中间设置一个VerticalLinearLayout来作为内容
        LinearLayout ll = new LinearLayout(context);
        ll.setOrientation(LinearLayout.VERTICAL);
        final int editTextCount = placeholderArray.size();
        final ArrayList<EditText> editTexts = new ArrayList<>();
        if(editTextCount>0) {
            for (int i = 0; i < editTextCount; i++) {
                EditText et = new EditText(context);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                String placeholder = placeholderArray.get(i);
                et.setHint(placeholder);
                if(defaultValueArray != null) {
                    String value = defaultValueArray.get(i);
                    et.setText(value);
                }
                ll.addView(et,layoutParams);
                editTexts.add(et);
            }
            inputDialog.setView(ll);
        }
        inputDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(callback != null) {
                            callback.onCancel();
                        }
                    }
                });
        inputDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    //which应该是有多个dialog时,用来区分是哪个dialog的.
                    public void onClick(DialogInterface dialog, int which) {
                        if(callback != null) {
                            final ArrayList<String> values = new ArrayList<>();
                                for (int i = 0; i < editTextCount; i++) {
                                    EditText et = editTexts.get(i);
                                    String value = et.getText().toString();
                                    values.add(value);
                                }
                            callback.onConfirm(values);
                        }
                    }
                });
        inputDialog.setCancelable(true);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ll.setLayoutParams(lp);
        inputDialog.show();
    }

    public static void showTextViewDialog(final Context context, final JsTextViewDialogCallback callback) {
        /*@setView 装入一个EditView
         */
        final TextView textView = new TextView(context);
        textView.setBackgroundColor(Color.parseColor("#ffcccccc"));

        AlertDialog.Builder inputDialog =
                new AlertDialog.Builder(context);
        inputDialog.setTitle("温馨提示");
        inputDialog.setView(textView);
        inputDialog.setMessage("请为视频输入标题");
        inputDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        callback.onClickCancel();
                    }
                });
        inputDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        callback.onClickOk(textView.getText().toString());
                    }
                });
        inputDialog.setCancelable(true);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,300);
        textView.setLayoutParams(lp);
        inputDialog.show();
    }


    //标题,消息
    //确定,取消.
    public static void showNormalDialog(final Context context, String title, String message, final JsNormalDialogCallback callback){
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(context);
//        normalDialog.setIcon(R.drawable.icon_dialog);
        normalDialog.setTitle(title);
        normalDialog.setMessage(message);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        callback.onClickOk();
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        callback.onClickCancel();
                    }
                });
        // 显示
        normalDialog.show();
    }

    @Override
    public void showAlert(Context context,String title, String message) {
        if(context == null) {
            return;
        }
        final AlertDialog.Builder dialogBuilder =
                new AlertDialog.Builder(context, R.style.Theme_AppCompat_Dialog_Alert);
        dialogBuilder.setTitle(title);
        dialogBuilder.setMessage(message);
        dialogBuilder.setPositiveButton("确定",null);

        // 显示
        dialogBuilder.show();
    }
}


